
package com.zkmonitor.util;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ParentPathParser {
    public static List<String> parse(String path) {
        if (path == null) {
            return Collections.emptyList();
        }
        List<String> parents = new ArrayList<>();
        String[] pathSegments = path.split("/");
        StringBuilder parent = new StringBuilder("");
        parents.add("/");
        for (String pathSegment : pathSegments) {
            if (StringUtils.isEmpty(pathSegment)) {
                continue;
            }
            parent.append("/");
            String[] dotPathSegment = pathSegment.split("\\.");
            for (int i = 0; i < dotPathSegment.length; i++) {
                if (i != 0) {
                    parent.append(".");
                }
                parent.append(dotPathSegment[i]);
                parents.add(parent.toString());
            }
        }
        return parents;
    }

    public static String getParent(String path) {
        int lastIndex = path.lastIndexOf('/');
        String parentPath = path.substring(0, lastIndex);
        return parentPath;
    }
}
